package autoteam.ui.dialogs;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import autoteam.tables.TeamTableModel;
import db.ColumnData;
import db.DBRecord;
import db.DataAccessObject;

public class TeamCreator extends AutoTeamDialog
{
	private static final long serialVersionUID = 6349959068539027867L;

	// String[] practiceNights = { "Monday", "Tuesday", "Wednesday", "Thursday",
	// "Friday" };


	private JButton addButton = new JButton("Add");
	private JButton closeButton = new JButton("Close");

	private JComboBox coachField;
	private JTextField teamField;
	private JTextField sportField;
	private JTextField leagueField;
	
	private TeamTableModel model;
	
	public TeamCreator(JFrame dialogOwner)
	{
		super(dialogOwner);

		
		this.setTitle("Add New Team");
		this.setDialogSize(new Dimension(300, 175));

		JPanel buttonPane = new JPanel();
		buttonPane.add(addButton);
		buttonPane.add(closeButton);
		setButtonActions(this);

		JPanel formPane = new JPanel(new GridLayout(4, 2));
		JLabel teamName = new JLabel("Team Name:");
		JLabel teamCoach = new JLabel("Team Coach:");
		JLabel sportType = new JLabel("Sport Type:");
		JLabel leagueName = new JLabel("League:");

		coachField = new JComboBox();
		DataAccessObject dao = new DataAccessObject();
		List<DBRecord> coachRecs = dao.getAllRecords("coaches");
		for (DBRecord rec : coachRecs)
		{
			coachField.addItem(rec.getFieldValue("firstname") +" "+ rec.getFieldValue("lastname"));
		}
		
		
		teamField = new JTextField(25);
		sportField = new JTextField(25);
		leagueField = new JTextField(25);
		
		
		sportField.setEditable(false);
		leagueField.setEditable(false);

		formPane.add(teamName);
		formPane.add(teamField);

		formPane.add(teamCoach);
		formPane.add(coachField);

		formPane.add(sportType);
		formPane.add(sportField);

		formPane.add(leagueName);
		formPane.add(leagueField);

		Container contentPane = getContentPane();

		contentPane.add(formPane, BorderLayout.CENTER);
		contentPane.add(buttonPane, BorderLayout.SOUTH);

		pack();
	}

	public TeamCreator(JFrame dialogOwner,TeamTableModel model)
	{
		this(dialogOwner);
		this.model = model;
	}
	
	@Override
	protected void setButtonActions(final AutoTeamDialog thisDialog)
	{
		addButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				// TODO ;l
				DataAccessObject dao = new DataAccessObject();
				
				dao.addRecord
				(
				     "teams", new ColumnData("teamName",teamField.getText()),
				     new ColumnData("coach",coachField.getSelectedIndex()),
				     new ColumnData("sport",sportField.getText())
				);
				
				model.add(teamField.getText());
				thisDialog.close();
			}
		});

		closeButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				thisDialog.close();
			}
		});
	}
}
